package com.bytz.modules.cms.auditBatch.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.auditBatch.model.AuditBatchCountModel;
import com.bytz.modules.cms.auditBatch.service.IAdminProductChangeService;
import com.bytz.modules.cms.auditBatch.service.IAuditBatchProductService;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.entity.ProductChange;
import com.bytz.modules.cms.product.model.ProductChangeExcel;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import com.bytz.modules.cms.user.entity.AuditBatch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "商品基准价格变更申请")
@RequestMapping("/product/change")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductChange.EDIT + "')")
public class ProductChangeController {

    @Autowired
    private IAuditBatchProductService auditBatchService;

    @Autowired
    private IAdminProductChangeService changeService;

    @ApiOperation(value = "申请分页查询", notes = "申请分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<AuditBatchCountModel>> page(AuditBatch auditBatch,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) throws Exception {

        auditBatch.setType(AuditBatchConstants.Type.PRODUCT);
        return Result.ok(auditBatchService.queryPage(new Page<>(pageNo, pageSize), auditBatch, req.getParameterMap()));
    }

    @ApiOperation(value = "产品其他信息修改分页查询", notes = "产品其他信息修改分页查询")
    @RequestMapping(value = "/otherList", method = RequestMethod.GET)
    public Result<IPage<AuditBatchCountModel>> otherList(AuditBatch auditBatch,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) throws Exception {

        auditBatch.setType(AuditBatchConstants.Type.RESELLER_PRODUCT_OTHER_CHANGE);
        return Result.ok(auditBatchService.queryPage(new Page<>(pageNo, pageSize), auditBatch, req.getParameterMap()));
    }

    @ApiOperation(value = "申请查询单条", notes = "申请查询单条")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<AuditBatch> queryById(@RequestParam String id) {

        return Result.ok(auditBatchService.getById(id));
    }

    @ApiOperation(value = "申请暂存", notes = "申请暂存")
    @RequestMapping(value = "/storage", method = RequestMethod.POST)
    public Result<AuditBatch> storage(@RequestBody AuditBatch auditBatch) {

        auditBatch.setStatus(AuditBatchConstants.Status.CAO_GAO);
        auditBatch.setType(AuditBatchConstants.Type.PRODUCT);
        return Result.ok(auditBatchService.storage(auditBatch));
    }

    @ApiOperation(value = "申请提交", notes = "申请提交")
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public Result<AuditBatch> submit(@RequestBody AuditBatch auditBatch) {

        auditBatch.setStatus(AuditBatchConstants.Status.DAI_SHENG_XIAO);
        auditBatch.setType(AuditBatchConstants.Type.PRODUCT);
        return Result.ok(auditBatchService.submit(auditBatch));
    }



    @ApiOperation(value = "商品其他信息暂存", notes = "商品其他信息暂存")
    @RequestMapping(value = "/otherStorage", method = RequestMethod.POST)
    public Result<AuditBatch> otherStorage(@RequestBody AuditBatch auditBatch) {

        auditBatch.setStatus(AuditBatchConstants.Status.CAO_GAO);
        auditBatch.setType(AuditBatchConstants.Type.RESELLER_PRODUCT_OTHER_CHANGE);
        return Result.ok(auditBatchService.storage(auditBatch));
    }

    @ApiOperation(value = "商品其他信息提交", notes = "商品其他信息提交")
    @RequestMapping(value = "/otherSubmit", method = RequestMethod.POST)
    public Result<AuditBatch> otherSubmit(@RequestBody AuditBatch auditBatch) {

        auditBatch.setStatus(AuditBatchConstants.Status.DAI_SHENG_XIAO);
        auditBatch.setType(AuditBatchConstants.Type.RESELLER_PRODUCT_OTHER_CHANGE);
        return Result.ok(auditBatchService.submit(auditBatch));
    }

    @ApiOperation(value = "申请删除", notes = "申请删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> deleteBatch(@RequestParam String id) {

        return Result.ok(auditBatchService.deleteBatch(id));
    }

    @ApiOperation(value = "变更记录分页查询", notes = "变更记录分页查询")
    @RequestMapping(value = "/change/list", method = RequestMethod.GET)
    public Result<IPage<ProductChange>> page(ProductChange change,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {

        return Result.ok(changeService.queryPage(new Page<>(pageNo, pageSize), change, req.getParameterMap()));
    }

    @ApiOperation(value = "变更记录导入模板", notes = "变更记录导入模板")
    @RequestMapping(value = "/change/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("价格变更申请导入模板", "Sheet1", ProductChange.class, response);
    }

    @ApiOperation(value = "导入产品其他信息模版", notes = "导入产品其他信息模版")
    @RequestMapping(value = "/change/otherTemplate", method = RequestMethod.POST)
    public void otherTemplate(HttpServletResponse response) throws Exception {
        ExcelUtil.exportTemp("商品信息批量维护模版", "Sheet1", ProductChangeExcel.class, response);
    }

    @ApiOperation(value = "变更记录导入", notes = "变更记录导入")
    @RequestMapping(value = "/change/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, @RequestParam String batchId) throws Exception {

        List<ProductChange> list = ExcelUtil.importGroup(request, ProductChange.class, null);
        changeService.importExcel(batchId, list);
        return Result.ok();
    }

    @ApiOperation(value = "导入产品其他信息到变更表", notes = "导入产品其他信息到变更表")
    @RequestMapping(value = "/change/importOtherExcel", method = RequestMethod.POST)
    public Result<?> importOtherExcel(HttpServletRequest request, @RequestParam String batchId){

        List<ProductChangeExcel> excelList = ExcelUtil.importGroup(request, ProductChangeExcel.class, null);
        List<ProductChange> productChangeList = BeanUtil.copyToList(excelList, ProductChange.class);
        changeService.importOtherExcel(batchId, productChangeList);
        return Result.ok();
    }

    @ApiOperation(value = "变更记录添加一条", notes = "变更记录添加一条")
    @RequestMapping(value = "/change/addOne", method = RequestMethod.POST)
    public Result<Boolean> addOne(@RequestBody ProductChange change) throws Exception {

        return Result.ok(changeService.saveChange(change));
    }

    @ApiOperation(value = "变更记录删除", notes = "变更记录删除")
    @RequestMapping(value = "/change/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(changeService.removeById(id));
    }

}
